﻿Imports MeoTom.Core

Partial Public Class Archive
    Inherits UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TitleName.InnerHtml = "Xem tất cả bài viết trong tháng " & Request.QueryString("Month") & " năm " & Request.QueryString("Year")

        Dim strTitle As String = "Xem tất cả bài viết trong tháng " & Request.QueryString("Month") & " năm " & Request.QueryString("Year")
        If Not Request.QueryString("Page") = vbNullString Then
            strTitle &= " - Trang " & Request.QueryString("Page")
        End If

        Page.Title = strTitle & " - " & Configs.WebTitle
    End Sub

    Public Function GenHtml(ByVal ArticlesID As String, ByVal Title As String, _
                        ByVal Abstract As String, ByVal TotalComment As String, _
                        ByVal ViewCount As String, ByVal ArticlePath As String, _
                        ByVal IsMedia As String, ByVal IsPicture As String, _
                        ByVal ReleaseDate As String, ByVal UserName As String, _
                        ByVal PostVisible As String) As String
        Dim strMedia As String = ""
        Dim strVideo As String = "&nbsp;<img src=""/images/hasVideo.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""15"" height=""9"" alt=""Video"" />"
        Dim strImage As String = "&nbsp;<img src=""/images/HasImage.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""10"" height=""10"" alt=""Images"" />"

        If IsMedia = "True" Then
            strMedia &= strVideo
        End If

        If IsPicture = "True" Then
            strMedia &= strImage
        End If

        Dim strOut As New StringBuilder
        If PostVisible = "2" Then
            strOut.Append(" <div class=""post-detail fl"">")
            strOut.Append("     <div class=""post-detail-left fl"">")
            strOut.Append("         <div class=""post-detail-bg""><span class=""total-cm"">").Append(TotalComment).Append("</span><br />Comments<br />").Append(ViewCount).Append(" Views</div>")
            strOut.Append("     </div>")
            strOut.Append("     <div class=""post-detail-right fl"">")
            strOut.Append("         <h3><a style=""color:#FF0000;"" href=""").Append(ArticlePath).Append(""" title=""").Append(Title).Append(""">").Append(Title).Append("</a>").Append(strMedia).Append("</h3>")
            strOut.Append("         <div class=""post-desc"">").Append(Server.HtmlDecode(Abstract).ToString).Append("...").Append("</div>")
            strOut.Append("         <div class=""post-info""><a href=""").Append(ArticlePath).Append(""" title=""").Append(Title).Append(""">").Append(Format(DateTime.Parse(ReleaseDate).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt")).Append("</a> bởi <a href=""/Users/").Append(UserName).Append(""" title=""").Append(UserName).Append(""">").Append(UserName).Append("</a> chuyên mục <span id=""ArticleCat___").Append(ArticlesID).Append("""></span></div>")
            strOut.Append("     </div>")
            strOut.Append(" </div>")
            strOut.Append(" <div class=""hr cut94 fl"">&nbsp;</div>")
        Else
            strOut.Append(" <div class=""post-detail fl"">")
            strOut.Append("     <div class=""post-detail-left fl"">")
            strOut.Append("         <div class=""post-detail-bg""><span class=""total-cm"">").Append(TotalComment).Append("</span><br />Comments<br />").Append(ViewCount).Append(" Views</div>")
            strOut.Append("     </div>")
            strOut.Append("     <div class=""post-detail-right fl"">")
            strOut.Append("         <h3><a href=""").Append(ArticlePath).Append(""" title=""").Append(Title).Append(""">").Append(Title).Append("</a>").Append(strMedia).Append("</h3>")
            strOut.Append("         <div class=""post-desc"">").Append(Server.HtmlDecode(Abstract).ToString).Append("...").Append("</div>")
            strOut.Append("         <div class=""post-info""><a href=""").Append(ArticlePath).Append(""" title=""").Append(Title).Append(""">").Append(Format(DateTime.Parse(ReleaseDate).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt")).Append("</a> bởi <a href=""/Users/").Append(UserName).Append(""" title=""").Append(UserName).Append(""">").Append(UserName).Append("</a> chuyên mục <span id=""ArticleCat___").Append(ArticlesID).Append("""></span></div>")
            strOut.Append("     </div>")
            strOut.Append(" </div>")
            strOut.Append(" <div class=""hr cut94 fl"">&nbsp;</div>")
        End If

        Return strOut.ToString
    End Function

    Private Sub lqDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles lqDataSource.Selecting
        If Request.QueryString("Year") = vbNullString Or Request.QueryString("Month") = vbNullString Then
            Response.Redirect("/Error?e=404")
        ElseIf IsNumeric(Request.QueryString("Year")) = False Or IsNumeric(Request.QueryString("Month")) = False Then
            Response.Redirect("/Error?e=404")
        Else
            Using dc As New MeoTomDataContext
                e.Result = dc.Justin_Article_Archive(Request.QueryString("Year"), Request.QueryString("Month"), Configs.SiteID, Configs.TimeLine).ToList
            End Using
        End If
    End Sub

    Private Sub lstArticle_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstArticle.PreRender
        If lstArticle.Items.Count = 0 Then
            Response.Redirect("/Error?e=NoArticle", False)
        End If
    End Sub

    Private Sub dtPgArticle_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtPgArticle.PreRender
        For Each control As UI.Control In dtPgArticle.Controls
            For Each c As UI.Control In control.Controls
                If TypeOf c Is HyperLink Then
                    Dim currentLink As HyperLink = DirectCast(c, HyperLink)
                    Dim match As Match = Regex.Match(currentLink.NavigateUrl, "Page=(?<Page123>.*?).*")
                    If match.Success Then
                        If Request.QueryString("Page") = vbNullString Then
                            currentLink.NavigateUrl = Request.RawUrl & "?" & match.Value
                        Else
                            currentLink.NavigateUrl = Request.RawUrl.Replace("Page=" & Request.QueryString("Page"), "") & match.Value
                        End If
                    End If
                End If
            Next
        Next
    End Sub
End Class